/*
 * Copyright 2020-2030 ALPHA LAB.Inc All Rights Reserved.
 */
package com.alphalab.framework.log;

import java.text.MessageFormat;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

/**
 * 日志接口实现.封装log4j实现.
 * @author allen
 * @version 1.0.0 2020年2月19日
 */
public class MoLogImpl implements IMoLog {
	
	/**
	 * log4j实例.通过构造方法初始化.
	 */
	Logger logger = null;
	
	/**
	 * MoLogImpl类名.
	 */
	private static final String FQCN = MoLogImpl.class.getName();
	
	/**
	 * 初始化log实例.
	 * @param className 类名
	 */
	public MoLogImpl(String className) {
		logger = Logger.getLogger(className); 
	}

	@Override
	public boolean isErrorEnabled() {
		return true;
	}
	
	@Override
	public boolean isWarnEnabled() {
		return true;
	}

	@Override
	public boolean isInfoEnabled() {
		return logger.isInfoEnabled();
	}

	@Override
	public boolean isDebugEnabled() {
		return logger.isDebugEnabled();
	}

	@Override
	public void error(Object paramObject) {
		logger.log(FQCN, Level.ERROR, paramObject, null);
	}

	@Override
	public void error(Throwable paramThrowable) {
		logger.log(FQCN, Level.ERROR, "", paramThrowable);
	}

	@Override
	public void error(String message, Object... params) {
		error(formatMsg(message, params));
	}

	@Override
	public void error(Object paramObject, Throwable paramThrowable) {
		logger.log(FQCN, Level.ERROR, paramObject, paramThrowable);
	}

	@Override
	public void warn(Object paramObject) {
		logger.log(FQCN, Level.WARN, paramObject, null);
	}

	@Override
	public void warn(String message, Object... params) {
		warn(formatMsg(message, params));
	}

	@Override
	public void warn(Object paramObject, Throwable paramThrowable) {
		logger.log(FQCN, Level.WARN, paramObject, paramThrowable);
	}

	@Override
	public void info(Object paramObject) {
		if (isInfoEnabled()) {
			logger.log(FQCN, Level.INFO, paramObject, null);
		}
	}

	@Override
	public void info(String message, Object... params) {
		info(formatMsg(message, params));
	}

	@Override
	public void debug(Object paramObject) {
		if (isDebugEnabled()) {
			logger.log(FQCN, Level.DEBUG, paramObject, null);
		}
	}

	@Override
	public void debug(String message, Object... params) {
		debug(formatMsg(message, params));
	}

	@Override
	public void debug(Object paramObject, Throwable paramThrowable) {
		if (isDebugEnabled()) {
			logger.log(FQCN, Level.DEBUG, paramObject, paramThrowable);
		}
	}

	/**
	 * 格式化字符串.赋值到占位符，params的值分别对应{0}、{1}...
	 * @param message
	 * @param params
	 * @return String
	 */
	private String formatMsg(String message, Object... params) {
		if ((params != null) && (params.length > 0)) {
			try {
				message = MessageFormat.format(message, params);
			} catch (Exception localException) {
			}
		}
		return message;
	}
	
}
